# -*- coding: utf-8 -*-
# TencentBlueKing is pleased to support the open source community by making
# 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available.
# Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
# Licensed under the MIT License (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
#
#     http://opensource.org/licenses/MIT
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific language governing permissions and
# limitations under the License.
#
# We undertake not to change the open source license (MIT license) applicable
# to the current version of the project delivered to anyone in the future.

# Generated by Django 2.2.17 on 2021-10-14 09:25
from urllib.parse import urlparse

from django.db import migrations, models
from django.conf import settings
from paasng.utils.blobstore import detect_default_blob_store


def forwards(apps, schema_editor):
    """extend full url"""
    store_type = detect_default_blob_store()
    SourcePackage = apps.get_model('sourcectl', 'SourcePackage')

    for package in SourcePackage.objects.all():
        o = urlparse(package.storage_path)
        if o.scheme == '':
            package.storage_url = f"{store_type.value}://{settings.BLOBSTORE_BUCKET_AP_PACKAGES}/{package.storage_path}"
            package.save(update_fields=["storage_url"])


class Migration(migrations.Migration):

    dependencies = [
        ('sourcectl', '0002_auto_20210422_1719'),
    ]

    operations = [
        migrations.AddField(
            model_name='sourcepackage',
            name='storage_url',
            field=models.CharField(default='', help_text='可获取到源码包的 URL 地址', max_length=1024, verbose_name='存储地址'),
            preserve_default=False,
        ),
        migrations.RunPython(forwards),
        migrations.AddField(
            model_name='sourcepackage',
            name='is_deleted',
            field=models.BooleanField(default=False, help_text='如果 SourcePackage 指向的源码包已被清理, 则设置该值为 True', verbose_name='源码包是否已被清理'),
        ),
        migrations.AlterField(
            model_name='sourcepackage',
            name='storage_path',
            field=models.CharField(help_text='[deprecated] 源码包在存储服务中存放的位置', max_length=1024, verbose_name='存储路径'),
        ),
    ]
